package com.lzh.java;

import javax.xml.crypto.Data;

/*
 * 算法考察：数组的复制，反转，查找（线性查找，二分查找）
 */

public class ArrayTest1 {
	
	public static void main(String[] args) {
		
		String[] arr1 = new String[] {"JJ","MM","AA","BB","CC"};
		
		//数组的复制
		String[] arr2 = new String[arr1.length];
		for(int i=0;i<arr1.length;i++) {
			arr2[i] = arr1[i];
		}
		
		//数组的反转
//		for(int i=0;i<arr1.length/2;i++) {
//			String temp = arr1[i];
//			arr1[i] = arr1[arr1.length - i -1];
//			arr1[arr1.length -i -1] = temp;
//		}
//		for(int i=0;i<arr1.length;i++) {
//			System.out.println(arr1[i]);
//		}
		
		
		//查找
		//线性查找
		String desc ="BB";
		
		boolean isFlag = true;
		for(int i=0;i<arr1.length;i++) {
			
			if(desc.equals(arr1[i])) {
				System.out.println("找到了指定的元素，位置为："+i);
				isFlag = false;
				break;
			}
			
		}
		if(isFlag) {
			System.out.println("没找到");
		}
		
		//二分法查找：折半查找
		//前提：所要查找的数组必须有序
		int[] arr3 = new int[] {-98,-34,2,34,54,66,79,105,210,333};
		
		int desc1 = -34;
		int head = 0;//初始的首索引
		int end = arr3.length - 1;//初始的末索引
		boolean isflag = true;
		while(head <= end) {
			int middle = (head + end)/2;
			
			if(desc1 == arr3[middle]) {
				System.out.println("找到了，位置为；"+middle);
				isflag = false;
				break;
			}else if(arr3[middle] > desc1) {
				end = middle - 1;
			}else {
				head = middle + 1;
			}
		}
		
		if(isflag) {
			System.out.println("很遗憾没找到");
		}
		
	}
	
}
